export default {
  namespaced: true,
  state: {
    address: uni.getStorageSync('address'),
    userInfo: uni.getStorageSync('userInfo'),
    token: uni.getStorageSync('token'),
    // 重定向的 object 对象 { openType, from }
    redirectInfo: null
  },
  getters: {
    getAddress (state) {
      return state.address
    },
    // 收货详细地址的计算属性
    addstr (state) {
      if (!state.address || !state.address.provinceName) return ''

      // 拼接 省，市，区，详细地址 的字符串并返回给用户
      return (
        state.address.provinceName +
        state.address.cityName +
        state.address.countyName +
        state.address.detailInfo
      )
    },
    getUserInfo (state) {
      return state.userInfo
    },
    getToken (state) {
      return state.token
    },
    getRedirectInfo (state) {
      return state.redirectInfo
    }
  },
  mutations: {
    updateAddress (state, address) {
      state.address = address

      // 2. 通过 this.commit() 方法，调用 m_user 模块下的 saveAddressToStorage 方法将 address 对象持久化存储到本地
      this.commit('m_user/saveAddressToStorage')
    },
    // 1. 定义将 address 持久化存储到本地 mutations 方法
    saveAddressToStorage (state) {
      uni.setStorageSync('address', state.address)
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo

      // 把用户信息保存到本地
      this.commit('m_user/saveUserInfoToStorage')
    },
    saveUserInfoToStorage (state) {
      uni.setStorageSync('userInfo', state.userInfo)
    },
    updateToken (state, token) {
      state.token = token

      // 把token保存到本地
      this.commit('m_user/saveTokenToStorage')
    },
    saveTokenToStorage (state) {
      uni.setStorageSync('token', state.token)
    },
    // 更新重定向的信息对象
    updateRedirectInfo (state, info) {
      state.redirectInfo = info
    }
  }
}
